﻿package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;

	public class PublishStream extends Sprite
	{
		private var video:Video;

		private var nc:NetConnection;

		private var ns:NetStream;

		private var cam:Camera;

		private var mic:Microphone;

		public function PublishStream():void
		{
			this.stage.addEventListener(MouseEvent.CLICK, onPublishClick);

			//获取视频和声音,并将视频显示到Flash界面
			cam = Camera.getCamera();
			cam.setMode(240, 180, 24);
			cam.setQuality(0, 90);

			mic = Microphone.getMicrophone();
			video = new Video(320, 240);
//			video = new Video(640, 480);
			video.attachCamera(cam);
			video.x = 20;
			video.y = 20;
			addChild(video);

		}

		private function onPublishClick(evt:MouseEvent):void
		{
			nc = new NetConnection();
//			nc.connect("rtmp://localhost/LiveStreams");
			nc.client = this
			nc.connect("rtmp://192.168.1.209/live");
			nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandler);
		}

		private function onNetStatusHandler(evt:NetStatusEvent):void
		{
			trace(evt.info.code);
			if (evt.info.code == "NetConnection.Connect.Success")
			{
				ns = new NetStream(nc);
				ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandler);
				ns.client = new CustomClient();
				ns.attachCamera(cam);
				ns.attachAudio(mic);
				ns.publish("stream", "live");
			}
		}

		public function onBWDone():void
		{
		}
	}
}
